﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniversalDataAccess.BusinessEntities;

namespace UniversalDataAccess.DataAccess
{
    public class GroupDataAccess : EducationalDivisionsDataAccess
    {
        public GroupDataAccess(String dbName, String conn, Boolean usePredefinedMetadata)
            : base(dbName, conn, usePredefinedMetadata)
        {
        }

        public List<Group> SelectGroupsList()
        {
            List<EducationalDivision> divList = SelectEducationalDivisionsList();

            List<AC_GROUP> grpList = dataContext.AC_GROUP.ToList();

            //Merge result
            var res = (from div in divList
                       join grp in grpList on div.ID equals grp.ID
                       select new Group
                       {
                           DataBaseName = dataBaseName,
                           CLASS_ID = div.CLASS_ID,
                           EXT_ID = div.EXT_ID,
                           ID = div.ID,
                           NAME = div.NAME,
                           PARENT_ID = div.PARENT_ID,
                           BOSS_ID = div.BOSS_ID,
                           DATE_CREATED = div.DATE_CREATED,
                           DESCRIPTION = div.DESCRIPTION,
                           CURATOR = grp.CURATOR_ID,
                           SHIFT = grp.SHIFT_ID
                       }).ToList();

            return res;
        }

        public Group SelectGroupBy_Id(int Id)
        {
            try
            {
                EducationalDivision basePart = SelectEducationalDivisionBy_Id(Id);

                List<AC_GROUP> grpPart = dataContext.AC_GROUP.ToList();

                //Merge result
                var res = (from grp in grpPart
                           where grp.ID == basePart.ID
                           select new Group
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = basePart.CLASS_ID,
                               EXT_ID = basePart.EXT_ID,
                               ID = basePart.ID,
                               NAME = basePart.NAME,
                               PARENT_ID = basePart.PARENT_ID,
                               BOSS_ID = basePart.BOSS_ID,
                               DATE_CREATED = basePart.DATE_CREATED,
                               DESCRIPTION = basePart.DESCRIPTION,
                               CURATOR = grp.CURATOR_ID,
                               SHIFT = grp.SHIFT_ID
                           }).FirstOrDefault();

                var cur = dataContext.TOBJECT.Where(t => t.EXT_ID == res.CURATOR).FirstOrDefault();
                res.CURATOR_NAME = cur != null ? cur.NAME : String.Empty; 

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectGroupById");
            }
        }

        public void InsertGroup(Group grp, ref int generated_Id)
        {
            try
            {
                InsertEducationalDivision(grp, ref generated_Id);

                int new_Id = generated_Id;
                AC_GROUP grpPart = new AC_GROUP();
                grpPart.DIVISION = dataContext.DIVISION.Where(t => t.ID == new_Id).FirstOrDefault();
                grpPart.ID = new_Id;
                grpPart.CURATOR_ID = grp.CURATOR;
                grpPart.SHIFT_ID = grp.SHIFT;

                //Submit changes
                dataContext.AddToAC_GROUP(grpPart);
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while InsertGroup");
            }
        }

        public void UpdateGroup(Group grp)
        {
            try
            {
                UpdateEducationalDivision(grp);

                AC_GROUP grpPart = dataContext.AC_GROUP.Where(t => t.ID == grp.ID).FirstOrDefault();

                grpPart.CURATOR_ID = grp.CURATOR;
                grpPart.SHIFT_ID = grp.SHIFT;

                //Submit changes
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while UpdateGroup");
            }
        }

        public void DeleteGroup(Group grp)
        {
            try
            {
                AC_GROUP grpPart = dataContext.AC_GROUP.Where(t => t.ID == grp.ID).FirstOrDefault();

                dataContext.DeleteObject(grpPart);
                dataContext.SaveChanges();

                DeleteEducationalDivision(grp);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while DeleteGroup");
            }
        }
    }
}
